package kr.co.doriclassic.mypuppystory.memo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import kr.co.doriclassic.mypuppystory.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;

@SuppressLint("ClickableViewAccessibility")
public class MultiMemoActivity extends Activity {
	static final int CALL_CAMERA = 1;
	static final int CALL_GALLERY = 2;
	static final int CALL_REC = 3;
	static final int CALL_RGALLERY = 4;

	ImageView mImage;
	StretchVideoView mVideo;
	EditText mText;
	String mPath;
	String mFile;
	Uri uImage;
	Uri uVideo;

	private MediaController mc;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multimemo);

		mImage = (ImageView) findViewById(R.id.attachimage);
		mPath = Environment.getExternalStorageDirectory().getAbsolutePath()
				+ "/attachimage.jpg";
		mVideo = (StretchVideoView) findViewById(R.id.videoview);
		mFile = Environment.getExternalStorageDirectory().getAbsolutePath()
				+ "/recvideo.mp4";
		mc = new MediaController(MultiMemoActivity.this);
		mVideo.setMediaController(mc);

		mText = (EditText) findViewById(R.id.edittext);

		controllerView();
	}// onCreate

	public void mOnClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.btncamera:
			intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(mPath)));
			startActivityForResult(intent, CALL_CAMERA);
			break;
		case R.id.btngallery:
			intent = new Intent(Intent.ACTION_PICK,
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(intent, CALL_GALLERY);
			break;
		case R.id.btnrec:
			intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
			startActivityForResult(intent, CALL_REC);
			break;
		case R.id.btnrgallery:
			intent = new Intent(Intent.ACTION_PICK,
					MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(intent, CALL_RGALLERY);
			break;
		case R.id.btngonu:// 공유
			// Log.d("text", mText.getText().toString().length()+"");

			
			if (mImage.getVisibility() == View.VISIBLE)
				createShareIntent("image/*|text/plain", uImage, mText.getText()
						.toString());
			else if (mVideo.getVisibility() == View.VISIBLE)
				createShareIntent("video/*|text/plain", uVideo, mText.getText()
						.toString());
			else
				Toast.makeText(getApplicationContext(), "사진 혹은 동영상을 불러오기하세요.",
						Toast.LENGTH_SHORT).show();
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case CALL_CAMERA:
				mImage.setVisibility(View.VISIBLE);
				mVideo.setVisibility(View.INVISIBLE);
				mImage.setImageBitmap(BitmapFactory.decodeFile(mPath));
				uImage = Uri.fromFile(new File(mPath));
				break;
			case CALL_GALLERY:
				try {
					mImage.setVisibility(View.VISIBLE);
					mVideo.setVisibility(View.INVISIBLE);
					mImage.setImageBitmap(MediaStore.Images.Media.getBitmap(
							getContentResolver(), data.getData()));
					uImage = data.getData();
				} catch (Exception e) {
					;
				}
				break;
			case CALL_REC:
				controllerView();
				mImage.setVisibility(View.INVISIBLE);
				mVideo.setVisibility(View.VISIBLE);
				mVideo.setVideoPath(data.getDataString());
				mVideo.pause();
				uVideo = Uri.fromFile(new File(data.getDataString()));
				break;
			case CALL_RGALLERY:
				controllerView();
				try {
					String path = getFileNameByUri(this, data.getData());
					mImage.setVisibility(View.INVISIBLE);
					mVideo.setVisibility(View.VISIBLE);
					mVideo.setVideoPath(path);
					mVideo.pause();
					uVideo = data.getData();
				} catch (Exception e) {
					;
				}
				break;
			}// switch
		}// if
	}// activityResult

	// shareIntent
	private void createShareIntent(String type, Uri mediaUri, String caption) {
		// Create the new Intent using the 'Send' action.
		Intent share = new Intent(Intent.ACTION_SEND);
		// Create the URI from the media
		Uri uri = mediaUri;

		// Set the MIME type
		share.setType(type);

		// Add the URI and the caption to the Intent.
		share.putExtra(Intent.EXTRA_STREAM, uri);
		share.putExtra(Intent.EXTRA_TEXT, caption);
		share.putExtra(Intent.EXTRA_SUBJECT, caption);
		share.putExtra(Intent.EXTRA_TITLE, caption);
		// Broadcast the Intent.
		startActivity(Intent.createChooser(share, null));
	}

	// 동영상 콘트롤러를 달기
	private void controllerView() {
		mVideo.postDelayed(new Runnable() {
			public void run() {
				mc.show(0);
			}
		}, 100);
	}// controllerView

	// 동영상갤러리를 사용하기 위한 메소드
	public static String getFileNameByUri(Context mContext, Uri selectedVideoUri) {
		String[] proj = { MediaStore.Images.Media.DATA };
		CursorLoader loader = new CursorLoader(mContext, selectedVideoUri,
				proj, null, null, null);
		Cursor cursor = loader.loadInBackground();
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}// getFileNameByUri

}